<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_agentmcp/model')->方法名();
*/
namespace jy_agentmcp;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//获取聊天文本
	public function getmsg($content,$isexi=false) {
		$basic=$this->plugin_setting('basic');
		$apitype=$basic['apitype'];
		if(!$apitype){
			if($isres){
				$res['choices'][0]['text']='chatAI暂时无法使用，请联系管理员，设置好基本参数：支持的AI';
				return $res;
			}else{
				exi('chatAI暂时无法使用，请联系管理员，设置好基本参数：支持的AI','error');
			}
		}
		$msg=model('jy_aiapi/model')->getmsg($content,$apitype,$isexi);
		if($msg){
			$this->upwenda($content,$msg);
		}
		return $msg;
	}
	//检测文本敏感内容
	public function checkcontent($content){
	    $result = model('core/wxapp_api')->msgSecCheck($content);
	    //有risky、pass、review三种值
	    // risky：有风险 review：建议人工先审后发  pass：通过
	    if($result['errcode']==0 && $result['result']['suggest']=='pass'){
	        return true;
	    }
	    return false;
	}
	//文本内容过滤接口(需要每月进行续费)
	public function text_filtration($chatmsg){
	    $key="0bus87SzpFE9kFNHwMkplfwBg6";
	    $result = model('core/ttapp_api')->api_text($key,$chatmsg);
	    $result = json_decode($result,true);
        if($result['code']==200 && $result['data']['conclusion_type']==1){
		    return true;
		}
        return false;
	}
	public function getwenda($msg=''){
		//完全匹配
		$wenda=pdo_get('jy_agentmcp_wenda',array('title'=>trim($msg),'pipei'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		if(!$wenda['msg']){
			$wenda=pdo_get('jy_agentmcp_wenda',array('title2 like'=>'%#'.trim($msg).'|%','pipei'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		}
		if(!$wenda['msg']){
			$wenda=pdo_get('jy_agentmcp_wenda',array('title2 like'=>'%|'.trim($msg).'|%','pipei'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		}
		if(!$wenda['msg']){
			$wenda=pdo_get('jy_agentmcp_wenda',array('title2 like'=>'%|'.trim($msg).'#%','pipei'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		}
		if(!$wenda['msg']){
			$wenda=pdo_get('jy_agentmcp_wenda',array('title like'=>'%'.trim($msg).'%','pipei'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		}
		if(!$wenda['msg']){
			$wenda=pdo_get('jy_agentmcp_wenda',array('title2 like'=>'%'.trim($msg).'%','pipei'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		}
		return $wenda['msg'];
	}
	//机器人入库
	public function upwenda($title='',$msg=''){
		$wenda=pdo_get('jy_agentmcp_wenda',array('title'=>trim($title),'uniacid'=>$_SESSION['uniacid']));
		if($wenda['id'] || !$title || !$msg){
			return false;
		}
		$basic=$this->plugin_setting('basic');
		if($basic['upwenda']==0){
		    return false;
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'title'=>$title,
			'msg'=>$msg,
			'status'=>$basic['wenda_status'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_agentmcp_wenda',$data);
		return true;
	}
}
?>